const { series, src, dest, watch } = require('gulp')
const htmlClean = require('gulp-htmlclean') // 压缩html
const less = require('gulp-less') // less转css
const cssClean = require('gulp-clean-css') // css压缩
const stripDebug = require('gulp-strip-debug') // 去除js调试语句 debugger console
const uglify = require('gulp-uglify') // js 压缩
const imgMin = require('gulp-imagemin') // 图片压缩
const gulpConnect = require('gulp-connect') // 服务器

const folder = {
    src: 'src/',
    dist: 'dist/'
}

// 任务:处理html  压缩html  
function html() {
    return src(folder.src + 'html/*')
        .pipe(htmlClean())
        .pipe(dest(folder.dist + 'html/'))
        .pipe(gulpConnect.reload()) // 重新加载
}

// 任务:处理css  less转css css压缩 
function css() {
    return src(folder.src + 'css/*')
        .pipe(less())
        .pipe(cssClean())
        .pipe(dest(folder.dist + 'css/'))
        .pipe(gulpConnect.reload()) // 重新加载
}

// 任务:处理js
function js(cb) {
    return src(folder.src + 'js/*')
        // .pipe(stripDebug())
        // .pipe(uglify())
        .pipe(dest(folder.dist + 'js/'))
        .pipe(gulpConnect.reload()) // 重新加载
}

// 任务:处理image
function image() {
    return src(folder.src + 'images/*')
        .pipe(imgMin())
        .pipe(dest(folder.dist + 'images/'))
}

function server(cb) {
    gulpConnect.server({ // 开启一个本地服务
        port: '1573',
		livereload: true,	//自动刷新
    })
    cb()
}

// 监听html修改
watch(folder.src + 'html/*', function (cb) {
    console.log('html修改')
    html()
    cb()
})
// 监听js修改
watch(folder.src + 'js/*', function (cb) {
    console.log('js修改')
    js()
    cb()
})
// 监听css修改
watch(folder.src + 'css/*', function (cb) {
    console.log('css修改')
    css()
    cb()
})

// 执行任务
exports.default = series(html, js, css , image, server) 